package com.exam.dao.teacher;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;

import com.exam.common.vo.ExamTeacherVO;
import com.exam.common.vo.page.PageModel;
import com.exam.common.vo.page.SystemContext;
import com.exam.dao.base.BaseDaoImpl;
import com.exam.entity.ExamTeacher;

@Repository("teacherDaoImp")
public class TeacherDaoImp extends BaseDaoImpl<ExamTeacher> implements TeacherDao{

	@SuppressWarnings("unchecked")
	@Override
	public PageModel<ExamTeacherVO> findByPage() {
		PageModel<ExamTeacherVO> pageModel = new PageModel<ExamTeacherVO>();
		int offset = SystemContext.getOffset();
		int pageSize = SystemContext.getPageSize();
		
		List<ExamTeacherVO> datas = new ArrayList<ExamTeacherVO>();
		
		List<ExamTeacher> tempDatas = this.getSession().createQuery(" from ExamTeacher ")
				.setFirstResult(offset).setMaxResults(pageSize)
				.list();
		for(ExamTeacher tea : tempDatas){
			ExamTeacherVO teaVO = new ExamTeacherVO();
			BeanUtils.copyProperties(tea, teaVO);
			datas.add(teaVO);
		}
		
		Long totalSize = (Long) this.getSession().createQuery("select count(*) from ExamTeacher").uniqueResult();
		
		pageModel.setTotalSize(totalSize);
		pageModel.setDatas(datas);
		pageModel.setOffset(offset);
		pageModel.setPageSize(pageSize);
		
		return pageModel;
	}

	@Override
	public ExamTeacherVO findByNumber(Integer teaNumber) {
		ExamTeacherVO teaVO = new ExamTeacherVO();
		ExamTeacher tea = (ExamTeacher) this.getSession().createQuery(" from ExamTeacher where examTeacherNumber=:teaNumber")
				.setParameter("teaNumber", teaNumber).uniqueResult();
		if(tea!=null){
			BeanUtils.copyProperties(tea, teaVO);
		}
		return teaVO;
	}

	
	@SuppressWarnings("unchecked")
	@Override
	public PageModel<ExamTeacherVO> findBySearcherTip(String searcherTip) {
		PageModel<ExamTeacherVO> page = new PageModel<ExamTeacherVO>();
		String hql = "from ExamTeacher where examTeacherName like '%"+searcherTip+"%' or examTeacherId like '%"
				+searcherTip+"%' or examTeacherNumber like '%"+searcherTip+"%'";
		int offset = SystemContext.getOffset();
		int pageSize = SystemContext.getPageSize();
		
		List<ExamTeacherVO> datas = new ArrayList<ExamTeacherVO>();
		
		List<ExamTeacher> templateData = this.getSession().createQuery(hql)
				.setFirstResult(offset).setMaxResults(pageSize).list();
		for(ExamTeacher teacher:templateData){
			ExamTeacherVO teacherVO = new ExamTeacherVO();
			BeanUtils.copyProperties(teacher, teacherVO);
			datas.add(teacherVO);
		}
		
		List<ExamTeacher> temp = this.getSession().createQuery(hql).list();
		
		page.setTotalSize(temp.size());
		page.setOffset(offset);
		page.setPageSize(pageSize);
		page.setDatas(datas);
		
		return page;
	}
	
}
